package com.gdones.bootserver.bus.paraminfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.bootserver.bus.paraminfo.dto.ParamInfo;
import com.gdones.bootserver.bus.paraminfo.mapper.ParamInfoMapper;
import com.gdones.bootserver.bus.paraminfo.service.IParamInfoService;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyResult;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典字段描述，值描述表 服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-03-27
 */
@Service
public class ParamInfoServiceImpl extends ServiceImpl<ParamInfoMapper, ParamInfo> implements IParamInfoService {

    @Override
    public MyResult getParamInfoPage(ParamInfo paramInfo, MyPageBand page) {
        MyResult result = new MyResult();

        ParamInfoMapper paramInfoMapper = getBaseMapper();

        QueryWrapper<ParamInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("paramID",paramInfo.getParamID());
        queryWrapper.orderByAsc("dataOrder");

        Page<ParamInfo> paramInfoPage = paramInfoMapper.selectPage(PageDTO.of(page.getPage(), page.getSize()), queryWrapper);

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("total",paramInfoPage.getTotal());
        resMap.put("pageData",paramInfoPage.getRecords());
        resMap.put("page",page.getPage());
        resMap.put("limit",page.getSize());

        result.setMsg("查询成功！");
        result.setData(resMap);

        return result;
    }

    @Override
    public MyResult add(ParamInfo paramInfo) {
        MyResult result = new MyResult();


        ParamInfoMapper paramInfoMapper = getBaseMapper();

        int insert = paramInfoMapper.insert(paramInfo);

        if(insert>0){
            result.setMsg("添加成功！");
        }else{
            result.setMsg("添加失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult update(ParamInfo paramInfo) {
        MyResult result = new MyResult();

        ParamInfoMapper paramInfoMapper = getBaseMapper();

        int update = paramInfoMapper.updateById(paramInfo);

        if(update>0){
            result.setMsg("修改成功！");
        }else{
            result.setMsg("修改失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult delete(String paramInfoIDs) {
        MyResult result = new MyResult();

        ParamInfoMapper paramInfoMapper = getBaseMapper();

        String[] split = paramInfoIDs.split(",");

        List<String> strings = Arrays.asList(split);

        QueryWrapper<ParamInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.in("paramInfoID",strings);

        int delete = paramInfoMapper.delete(queryWrapper);

        if(delete>0){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }
}
